﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace Steamrunner
{
    class HighscoreScreen : Screen
    {
        Texture2D background = null;
        Texture2D title = null;

        Highscore highscore = null;
        SpriteFont font = null;

        string nextScreen;

        public Rectangle screenBoundaries;

        public HighscoreScreen(ScreenManager screenManager, Game game)
            : base(screenManager, game)
        {
        }

        public override void Initialize()
        {
            highscore = new Highscore("highscore.ini");
            highscore.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            screenBoundaries = Game.Window.ClientBounds;

            font = Game.Content.Load<SpriteFont>(@"Fonts\highscoreFont");

            guiManager = new GuiManager(Game);

            Button button = new Button(Game, Spritebatch, "Images\\Menus\\h_btmm", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.85f), "MenuScreen");
            guiManager.AddButton(button);

            guiManager.Initialize();

            background = Game.Content.Load<Texture2D>(@"Images\Menus\background");
            title = Game.Content.Load<Texture2D>(@"Images\Menus\h_title");
        }

        public override void Update(GameTime gameTime)
        {
            musicManager.Update(gameTime);

            inputManager.Update(gameTime);

            if (inputManager.JustPressed(Keys.W))
            {
                guiManager.JoystickUp();
                musicManager.PlaySound("buttonScrolling");
            }
            else if (inputManager.JustPressed(Keys.S))
            {
                guiManager.JoystickDown();
                musicManager.PlaySound("buttonScrolling");
            }

            if (inputManager.JustPressed(Keys.A) || inputManager.JustPressed(Keys.D))
            {
                nextScreen = guiManager.ButtonClicked();
                musicManager.PlaySound("buttonSelect");
                screenManager.CurrentScreen = nextScreen;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Spritebatch.Begin();

            Spritebatch.Draw(background, Vector2.Zero, Color.White);

            Spritebatch.Draw(title, Vector2.Zero, Color.White);

            Color color = new Color(4, 88, 120);
            Vector2 position = new Vector2(100f, 220f);
            for (int i = 0; i < 10; i++)
            {
                Spritebatch.DrawString(font, (i+1).ToString(), new Vector2(position.X - (font.MeasureString((i+1).ToString()).X * 0.5f), position.Y), color);
                string name = highscore.Name(i).Substring(0, 4);
                Spritebatch.DrawString(font, name, new Vector2(screenBoundaries.Width * 0.5f - (font.MeasureString(name).X * 0.5f), position.Y), color);
                string time = highscore.Score(highscore.Name(i)).ToString();
                string printTime = "";
                if (time.Length == 6)
                {
                    printTime = time.Substring(0, 1) + ":" + time.Substring(1, 2) + ":" + time.Substring(3);
                }
                else
                {
                    printTime = "0" + ":" + time.Substring(0, 2) + ":" + time.Substring(2);
                }
                
                Spritebatch.DrawString(font, printTime, new Vector2(screenBoundaries.Width - font.MeasureString(printTime).X - 30f, position.Y), color);
                position.Y += 53f;
            }

            guiManager.Draw(gameTime);

            Spritebatch.End();

            base.Draw(gameTime);
        }

        public override string GetNextScreen()
        {
            return nextScreen;
        }
    }
}
